<template>
  <el-dialog
    :title="!dataForm.housingRingId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="经纪人id" prop="brokerId">
      <el-input v-model="dataForm.brokerId" placeholder="经纪人id"></el-input>
    </el-form-item>
    <el-form-item label="楼市圈名称" prop="housingRingName">
      <el-input v-model="dataForm.housingRingName" placeholder="楼市圈名称"></el-input>
    </el-form-item>
    <el-form-item label="楼市圈地址" prop="housingRingAddress">
      <el-input v-model="dataForm.housingRingAddress" placeholder="楼市圈地址"></el-input>
    </el-form-item>
    <el-form-item label="经纪人名称" prop="brokerName">
      <el-input v-model="dataForm.brokerName" placeholder="经纪人名称"></el-input>
    </el-form-item>
    <el-form-item label="楼市圈平均价格" prop="housingRingPrice">
      <el-input v-model="dataForm.housingRingPrice" placeholder="楼市圈平均价格"></el-input>
    </el-form-item>
    <el-form-item label="市的id(地址的id)" prop="shiId">
      <el-input v-model="dataForm.shiId" placeholder="市的id(地址的id)"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          housingRingId: 0,
          brokerId: '',
          housingRingName: '',
          housingRingAddress: '',
          brokerName: '',
          housingRingPrice: '',
          shiId: ''
        },
        dataRule: {
          brokerId: [
            { required: true, message: '经纪人id不能为空', trigger: 'blur' }
          ],
          housingRingName: [
            { required: true, message: '楼市圈名称不能为空', trigger: 'blur' }
          ],
          housingRingAddress: [
            { required: true, message: '楼市圈地址不能为空', trigger: 'blur' }
          ],
          brokerName: [
            { required: true, message: '经纪人名称不能为空', trigger: 'blur' }
          ],
          housingRingPrice: [
            { required: true, message: '楼市圈平均价格不能为空', trigger: 'blur' }
          ],
          shiId: [
            { required: true, message: '市的id(地址的id)不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.housingRingId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.housingRingId) {
            this.$http({
              url: this.$http.adornUrl(`/publish/housingring/info/${this.dataForm.housingRingId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.brokerId = data.housingRing.brokerId
                this.dataForm.housingRingName = data.housingRing.housingRingName
                this.dataForm.housingRingAddress = data.housingRing.housingRingAddress
                this.dataForm.brokerName = data.housingRing.brokerName
                this.dataForm.housingRingPrice = data.housingRing.housingRingPrice
                this.dataForm.shiId = data.housingRing.shiId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/publish/housingring/${!this.dataForm.housingRingId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'housingRingId': this.dataForm.housingRingId || undefined,
                'brokerId': this.dataForm.brokerId,
                'housingRingName': this.dataForm.housingRingName,
                'housingRingAddress': this.dataForm.housingRingAddress,
                'brokerName': this.dataForm.brokerName,
                'housingRingPrice': this.dataForm.housingRingPrice,
                'shiId': this.dataForm.shiId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
